package br.com.ldml;

import java.util.ArrayList;

import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

public class Recuperar {
	private String repositorio;
	private String uri;
	
	public String getRepositorio() {
		return repositorio;
	}
	
	public void setRepositorio(String repositorio) {
		this.repositorio = repositorio;
	}
	
	public String getUris() {
		return uri;
	}
	
	public void setUris(String uris) {
		this.uri = uris;
	}
	
	public Tripla[] exec() {
		ArrayList<Tripla> t = new ArrayList<Tripla>();
		
		//sparql query
		String sparqlQueryString = 					
				"SELECT DISTINCT ?a ?b WHERE { ?a ?b <"+ uri +"> } LIMIT 100";
		
		//endpoint config
		Query query = QueryFactory.create(sparqlQueryString);
		QueryExecution qexec = QueryExecutionFactory.sparqlService(repositorio, query);

		//print result
		ResultSet results = qexec.execSelect();
		for(QuerySolution qs : ResultSetFormatter.toList(results)) {
			Tripla t1 = new Tripla(qs.get("a").toString(), qs.get("b").toString(), uri);	
			t.add(t1);
		}

		//closing connection
		qexec.close() ;
		
		Tripla[] aux = new Tripla[t.size()];
		aux = t.toArray(aux);
		return aux;
	}
}
